import sys
import pkg_resources
import os

print("检查Python环境信息...")
print(f"Python版本: {sys.version}")
print(f"Python解释器路径: {sys.executable}")
print(f"当前工作目录: {os.getcwd()}")
print(f"Python模块搜索路径: ")
for path in sys.path:
    print(f"  - {path}")

print("\n检查已安装的langchain相关包:")
try:
    # 列出所有已安装的包，查找与langchain相关的
    installed_packages = pkg_resources.working_set
    langchain_packages = [pkg for pkg in installed_packages if "langchain" in pkg.key]
    
    if langchain_packages:
        for pkg in langchain_packages:
            print(f"✅ 找到包: {pkg.key} (版本: {pkg.version})")
            # 尝试获取包的安装位置
            try:
                dist = pkg_resources.get_distribution(pkg.key)
                print(f"  安装位置: {dist.location}")
            except Exception as e:
                print(f"  无法获取安装位置: {e}")
    else:
        print("❌ 未找到任何langchain相关包")

except Exception as e:
    print(f"检查包时出错: {e}")

print("\n尝试直接查找langchain模块的位置:")
try:
    import langchain
    print(f"✅ 成功导入langchain模块")
    print(f"  langchain模块位置: {langchain.__file__}")
    
    # 尝试列出langchain的所有子模块
    print("  langchain子模块列表:")
    for attr in dir(langchain):
        if not attr.startswith('__'):
            submodule = getattr(langchain, attr, None)
            if hasattr(submodule, '__file__'):
                print(f"    - {attr}: {submodule.__file__}")
            elif hasattr(submodule, '__path__'):
                print(f"    - {attr}: {submodule.__path__}")

except ImportError as e:
    print(f"❌ 导入langchain失败: {e}")

print("\n尝试直接导入langchain.core模块:")
try:
    import langchain.core
    print(f"✅ 成功导入langchain.core模块")
    print(f"  langchain.core模块位置: {langchain.core.__file__}")
    
    # 尝试列出core模块的子模块
    print("  langchain.core子模块列表:")
    for attr in dir(langchain.core):
        if not attr.startswith('__'):
            submodule = getattr(langchain.core, attr, None)
            if hasattr(submodule, '__file__'):
                print(f"    - {attr}: {submodule.__file__}")
            elif hasattr(submodule, '__path__'):
                print(f"    - {attr}: {submodule.__path__}")

except ImportError as e:
    print(f"❌ 导入langchain.core失败: {e}")

print("\n尝试直接导入langchain.core.messages模块:")
try:
    import langchain.core.messages
    print(f"✅ 成功导入langchain.core.messages模块")
    print(f"  langchain.core.messages模块位置: {langchain.core.messages.__file__}")
    
    # 检查是否有AnyMessage类
    if hasattr(langchain.core.messages, 'AnyMessage'):
        print("  ✅ 找到AnyMessage类")
    else:
        print("  ❌ 未找到AnyMessage类")
        # 列出messages模块中的所有类
        print("  messages模块中的类:")
        for attr in dir(langchain.core.messages):
            if not attr.startswith('__') and isinstance(getattr(langchain.core.messages, attr), type):
                print(f"    - {attr}")

except ImportError as e:
    print(f"❌ 导入langchain.core.messages失败: {e}")

print("\n提供的解决方案:")
print("1. 尝试重新安装langchain的特定版本")
print("2. 检查是否有多个Python环境冲突")
print("3. 考虑创建一个新的虚拟环境")
print("\n建议的安装命令:")
print("# 重新安装最新版langchain")
print(f"{sys.executable} -m pip install --upgrade --force-reinstall langchain -i https://mirrors.aliyun.com/pypi/simple")
print("\n# 或者安装特定版本")
print(f"{sys.executable} -m pip install --force-reinstall langchain==0.1.13 -i https://mirrors.aliyun.com/pypi/simple")